package com.cj.st.sevice.impl;

import com.cj.st.dao.UserInfoDao;
import com.cj.st.entity.UserInfo;
import com.cj.st.sevice.UserInfoService;
import com.cj.st.util.Consts;
import com.cj.st.util.ReturnT;
import io.swagger.models.auth.In;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserInfoServiceImpl implements UserInfoService {

    @Autowired
    private UserInfoDao userInfoDao;

    @Override
    public UserInfo findByUsername(String username) {
        return userInfoDao.findByUsername(username);
    }

    @Override
    public ReturnT save(UserInfo userInfo) {
//        if (userInfo.getUid() == null) {
//            return ReturnT.FAIL;
//        }
//        UserInfo info = findOne(userInfo.getUid());
//        if (info == null) {
//            return ReturnT.FAIL;
//        }
        byte[] credentials = userInfo.getPassword().getBytes();
        userInfo.setSalt(userInfo.getPassword());
        ByteSource credentialsSalt = ByteSource.Util.bytes(userInfo.getUsername() + userInfo.getSalt());
        Object obj = new SimpleHash(Consts.hashAlgorithmName, credentials, credentialsSalt, Consts.hashIterations);

        userInfo.setPassword((String) obj);
        System.out.println(obj);

        userInfoDao.save(userInfo);
        return ReturnT.SUCCESS;
    }

    @Override
    public ReturnT update(UserInfo userInfo) {
        userInfoDao.save(userInfo);
        return ReturnT.SUCCESS;
    }

    @Override
    public ReturnT delete(Object var) {
        userInfoDao.delete((Integer) var);
        return ReturnT.SUCCESS;
    }

    @Override
    public List<UserInfo> getAll() {
        return userInfoDao.findAll();
    }

    @Override
    public UserInfo findOne(Object id) {
        return userInfoDao.findOne((Integer) id);
    }
}